<?php

class Password {
	const min_chars = 6;
	const max_chars = 16;
	
	private $password = "";
	
	/**
	 * @param string $str
	 */
	public function __construct($str) {
		if ($this->isPasswordOk($str)) {
			$this->password = sha1($str);
		}
	}
	
	/**
	 * @return string
	 */
	public function __toString() {
		return $this->password;
	}
	
	/**
	 * @param string $password
	 * @param string $otherPassword
	 * 
	 * @return boolean
	 */
	public function doPasswordMatch($password, $otherPassword) {
		if ($password != $otherPassword) {
			throw new \Exception("Passwords does not match.");
		}
		return true;
	}
	
	/**
	 * @param string $str
	 * 
	 * @return boolean
	 */
	private function isPasswordOk($str) {
		if (strlen($str) < self::min_chars) {
			throw new \Exception("Password too short (min ".self::min_chars." characters)");
		} 
		else if (strlen($str) > self::max_chars) {
			throw new \Exception("Password too long (min ".self::max_chars." characters)");
		}
		return true;
	}
}
